package com.example.web1012.controller;

import com.example.web1012.Dao.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @projectName: web10-12
 * @package: com.example.web1012.controller
 * @className: UserLogin
 * @author: Berry Peng
 * @description: 用户登录
 * @date: 2022/10/23 16:01
 * @version: 1.0
 */
@Controller
@RequestMapping("/login")
public class UserLogin {
    @RequestMapping("/userLogin")
    public void userLogin(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, User> userMap = new HashMap<>();
        userMap.put("tom", new User("tom","123ryu","tom@sohu.com"));
        userMap.put("jack", new User("jack","789asd","jack@163.com"));
        userMap.put("wang", new User("wang","456zxc","wang@163.com"));
        
        if (userMap.get(user.getUserName()) != null&& Objects.equals(userMap.get(user.getUserName()).getPassword(), user.getPassword())) {
            request.getSession().setAttribute(user.getUserName(),user);
            request.getSession().setAttribute("isLogin",true);
            response.sendRedirect("/index.html");
        }else{
            response.sendRedirect("/login.html");
        }
        
    }
}
